﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using Facade;
using IronOne.SalesOne.BusinessObjects;

namespace IronOne.SalesOne.SalesOneWeb.ProductLibrary
{
    public partial class ManageProducts1 : SOSecurityPage
    {
        private String action = "create";
        private static int codeIdd = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                ValidateUserCredentials(Request.Url.LocalPath);

                if (!String.IsNullOrEmpty(Request.QueryString["a"]))
                {
                    action = Request.QueryString["a"].ToString();
                }

                if (action == "u")
                {
                    this.LoadData();
                }
                else if (action == "v")
                {
                    this.LoadData();
                    btnUpdate.Visible = false;
                    //this.Form.FindControl("btn").Visible = false;
                }
            }

        }

        private void LoadData()
        {
            try
            {
                string id1 = (Session["productCode"].ToString());
                FinanceFacade productfacade = new FinanceFacade();
                List<Product> product = productfacade.GetProduct(id1);

                if (action == "v")
                {
                    //btnUpdate.Visible = false;

                    //Button btnUp = (Button)this.FindControl("btnUpdate");
                    //btnUp.Visible = false;
                    //this.Form.FindControl("btn").Visible = false;
                    textBox.Attributes["style"] = "display:none";
                    btnCancel.Visible = true;

                    //btnUpdate.Attributes["style"] = "display:none"; 
                    btnUp.Attributes["style"] = "display:none";
                    //System.Web.UI.HtmlControls.HtmlTableCell TestCell = (System.Web.UI.HtmlControls.HtmlTableCell)this.FindControl("ctl00_ContentPlaceHolder1_btnUp");
                    //Button btn = (Button)this.FindControl("ctl00_ContentPlaceHolder1_btnUploadFile");
                    //btn.Visible = false;
                    //TestCell.Attributes["style"] = "display:none"; 
                   
                }
                else if (action == "u")
                {
                    lable.Attributes["style"] = "display:none";
                    btnUpdate.Visible = true;
                    //this.Form.FindControl("btn").Visible = true;
                    btnCancel.Visible = true;
                }

                codeIdd = (int)(product.First().ProductCategoryCode);
                lblPlanCategory.Text = product.First().ProductCategoryName;


                if (action == "v")
                {
                    lblPlanCode.Text = product.First().PlanCode;
                    txtPlanCode.Visible = false;
                    //txtPlanCode.Attributes.Add("style",":none");
                }
                else if (action == "u")
                {
                    txtPlanCode.Text = product.First().PlanCode;
                    txtPlanCode.Enabled = false;
                    lblPlanCode.Visible = false;
                }


                if (action == "v")
                {
                    lblPlanName.Text = product.First().PlanName;
                    txtPlanName.Visible = false;
                }
                else if (action == "u")
                {
                    txtPlanName.Text = product.First().PlanName;
                    lblPlanName.Visible = false;
                }


                if (action == "v")
                {
                    //string temp = e.Row.Cells[i].Text;
                    //e.Row.Cells[i].Text = String.Format("{0:0,0.00}", float.Parse(temp));
                    string maxPrem = (product.First().PlanMaxPremium).ToString();
                    lblPremiumMax.Text = String.Format("{0:0,0.00}", float.Parse(maxPrem));
                    string minPrem = (product.First().PlanMinPremium).ToString();
                    lblPremiumMin.Text = String.Format("{0:0,0.00}", float.Parse(minPrem)); 

                    lblAgeMax.Text = (product.First().PlanMaxAge).ToString();
                    lblAgeMin.Text = (product.First().PlanMinAge).ToString();
                    lblTermMax.Text = (product.First().PlanMaxTerm).ToString();
                    lblTermMin.Text = (product.First().PlanMinTerm).ToString();                    
                    
                    txtAgeFrom.Visible = false;
                    txtAgeTo.Visible = false;
                    txtTermFrom.Visible = false;
                    txtTermTo.Visible = false;
                    txtPremiumFrom.Visible = false;
                    txtPremiumTo.Visible = false;

                    



                }
                else if (action == "u")
                {
                    txtAgeFrom.Text = (product.First().PlanMinAge).ToString();
                    txtAgeTo.Text = (product.First().PlanMaxAge).ToString();
                    txtTermFrom.Text = (product.First().PlanMinTerm).ToString();
                    txtTermTo.Text = (product.First().PlanMaxTerm).ToString();

                    string minPrem = (product.First().PlanMinPremium).ToString();
                    txtPremiumFrom.Text = String.Format("{0:0,0.00}", float.Parse(minPrem));
                    
                    string maxPrem = (product.First().PlanMaxPremium).ToString();
                    txtPremiumTo.Text = String.Format("{0:0,0.00}", float.Parse(maxPrem));

                    //txtPremiumFrom.Text = (product.First().PlanMinPremium).ToString();
                    //txtPremiumTo.Text = (product.First().PlanMaxPremium).ToString();

                    lblAgeMin.Visible = false;
                    lblAgeMax.Visible = false;
                    lblTermMin.Visible = false;
                    lblTermMax.Visible = false;
                    lblPremiumMin.Visible = false;
                    lblPremiumMax.Visible = false;

                }

                if (action == "v")
                {
                    chMonthly.Enabled = false;
                    if (product.First().MonthlyMode == "A")
                    {
                        chMonthly.Checked = true;
                    }
                    else if (product.First().MonthlyMode == "N")
                    {
                        chMonthly.Checked = false;
                    }
                    else
                    {
                        chMonthly.Checked = false;
                    }

                }
                else if (action == "u")
                {
                    chMonthly.Enabled = true;
                    if (product.First().MonthlyMode == "A")
                    {
                        chMonthly.Checked = true;
                    }
                    else if (product.First().MonthlyMode == "N")
                    {
                        chMonthly.Checked = false;
                    }
                    else
                    {
                        chMonthly.Checked = false;
                    }

                }



                if (action == "v")
                {
                    chQuarterly.Enabled = false;
                    if (product.First().QuarterlyMode == "A")
                    {
                        chQuarterly.Checked = true;
                    }
                    else if (product.First().QuarterlyMode == "N")
                    {
                        chQuarterly.Checked = false;
                    }
                    else
                    {
                        chQuarterly.Checked = false;
                    }
                }
                else if (action == "u")
                {
                    chQuarterly.Enabled = true;
                    if (product.First().QuarterlyMode == "A")
                    {
                        chQuarterly.Checked = true;
                    }
                    else if (product.First().QuarterlyMode == "N")
                    {
                        chQuarterly.Checked = false;
                    }
                    else
                    {
                        chQuarterly.Checked = false;
                    }
                }

                if (action == "v")
                {
                    chYearly12.Enabled = false;
                    if (product.First().YearlyMode == "A")
                    {
                        chYearly12.Checked = true;
                    }
                    else if (product.First().YearlyMode == "N")
                    {
                        chYearly12.Checked = false;
                    }
                    else
                    {
                        chYearly12.Checked = false;
                    }
                }
                else if (action == "u")
                {
                    chYearly12.Enabled = true;
                    if (product.First().YearlyMode == "A")
                    {
                        chYearly12.Checked = true;
                    }
                    else if (product.First().YearlyMode == "N")
                    {
                        chYearly12.Checked = false;
                    }
                    else
                    {
                        chYearly12.Checked = false;
                    }
                }

                if (action == "v")
                {
                    chYearly6.Enabled = false;
                    if (product.First().HalfYearlyMode == "A")
                    {
                        chYearly6.Checked = true;
                    }
                    else if (product.First().HalfYearlyMode == "N")
                    {
                        chYearly6.Checked = false;
                    }
                    else
                    {
                        chYearly6.Checked = false;
                    }
                }
                else if (action == "u")
                {
                    chYearly6.Enabled = true;
                    if (product.First().HalfYearlyMode == "A")
                    {
                        chYearly6.Checked = true;
                    }
                    else if (product.First().HalfYearlyMode == "N")
                    {
                        chYearly6.Checked = false;
                    }
                    else
                    {
                        chYearly6.Checked = false;
                    }
                }


                if (action == "v")
                {
                    chSingle.Enabled = false;
                    if (product.First().SingleMode == "A")
                    {
                        chSingle.Checked = true;
                    }
                    else if (product.First().SingleMode == "N")
                    {
                        chSingle.Checked = false;
                    }
                    else
                    {
                        chSingle.Checked = false;
                    }
                }
                else if (action == "u")
                {
                    chSingle.Enabled = true;
                    if (product.First().SingleMode == "A")
                    {
                        chSingle.Checked = true;
                    }
                    else if (product.First().SingleMode == "N")
                    {
                        chSingle.Checked = false;
                    }
                    else
                    {
                        chSingle.Checked = false;
                    }
                }


                if (action == "v")
                {
                    rdoYes.Enabled = false;
                    rdoNo.Enabled = false;

                    if (product.First().PlanReport == "A")
                    {
                        rdoYes.Checked = true;
                    }
                    else
                    {
                        rdoNo.Checked = true;
                    }

                }
                else if (action == "u")
                {
                    rdoYes.Enabled = true;
                    rdoNo.Enabled = true;

                    if (product.First().PlanReport == "A")
                    {
                        rdoYes.Checked = true;
                    }
                    else
                    {
                        rdoNo.Checked = true;
                    }
                }

                if (action == "v")
                {
                    txtDescriptionArea.Enabled = false;
                    btnBrowse.Visible = false;
                    btnUploadFile.Visible = false;
                    txtDescription.Visible = false;
                    txtDescriptionArea.Text = product.First().ProductDescription;
                }
                else if (action == "u")
                {
                    txtDescriptionArea.Text = product.First().ProductDescription;
                    btnBrowse.Visible = false;
                    btnUploadFile.Visible = false;
                    txtDescription.Visible = false;
                }

                if (action == "v")
                {
                    rdoActive.Enabled = false;
                    rdoInactive.Enabled = false;

                    if (product.First().Status == "A")
                    {
                        rdoActive.Checked = true;
                    }
                    else
                    {
                        rdoInactive.Checked = true;
                    }
                   
                }
                else if (action == "u")
                {
                    rdoActive.Enabled = true;
                    rdoInactive.Enabled = true;

                    if (product.First().Status == "A")
                    {
                        rdoActive.Checked = true;
                    }
                    else
                    {
                        rdoInactive.Checked = true;
                    }
                }

            }
            catch (Exception ex)
            {
                return;
            }

        }


        private void Update()
        {
            FinanceFacade pFac = new FinanceFacade();
            Product qlb = pFac.GetOneProduct(Session["productCode"].ToString());


            qlb.ProductCategoryName = lblPlanCategory.Text;
            qlb.PlanCode = txtPlanCode.Text;
            qlb.PlanName = txtPlanName.Text;
            qlb.PlanMaxAge = Convert.ToInt32((txtAgeTo.Text).ToString());
            qlb.PlanMinAge = Convert.ToInt32((txtAgeFrom.Text).ToString());
            qlb.PlanMaxTerm = Convert.ToInt32((txtTermTo.Text).ToString());
            qlb.PlanMinTerm = Convert.ToInt32((txtTermFrom.Text).ToString());
            qlb.PlanMaxPremium = (float)(Convert.ToDecimal((txtPremiumTo.Text).ToString()));
            qlb.PlanMinPremium = (float)(Convert.ToDecimal((txtPremiumFrom.Text).ToString()));


            if (chMonthly.Checked == true)
            {
                qlb.MonthlyMode = "A";
            }
            else
            {
                qlb.MonthlyMode = "N";
            }


            if (chQuarterly.Checked == true)
            {
                qlb.QuarterlyMode = "A";
            }
            else
            {
                qlb.QuarterlyMode = "N";
            }


            if (chYearly12.Checked == true)
            {
                qlb.YearlyMode = "A";
            }
            else
            {
                qlb.YearlyMode = "N";
            }


            if (chYearly6.Checked == true)
            {
                qlb.HalfYearlyMode = "A";
            }
            else
            {
                qlb.HalfYearlyMode = "N";
            }

            if (chSingle.Checked == true)
            {
                qlb.SingleMode = "A";
            }
            else
            {
                qlb.SingleMode = "N";
            }

            if (rdoYes.Checked == true)
            {
                qlb.PlanReport = "A";
            }
            else
            {
                qlb.PlanReport = "N";
            }

            if (rdoNo.Checked == true)
            {
                qlb.PlanReport = "N";
            }
            else
            {
                qlb.PlanReport = "A";
            }

            qlb.ProductDescription = txtDescriptionArea.Text;


            if (rdoActive.Checked == true)
            {
                qlb.Status = "A";
            }
            else
            {
                qlb.Status = "N";
            }

            if (rdoInactive.Checked == true)
            {
                qlb.Status = "N";
            }
            else
            {
                qlb.Status = "A";
            }


            int successUpdate = pFac.UpdateProductTrans(qlb);

            if (successUpdate == 1)
            {
                Session["successCodeUpdate"] = "Successfully updated ";
            }
            else
            {
                Session["failCodeUpdate"] = "Failed to update ";
            }

        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Session["productCode"] = null;
            Response.Redirect("~/ProductLibrary/SearchProducts.aspx");
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            if ((chMonthly.Checked == false && chQuarterly.Checked == false && chSingle.Checked == false && chYearly12.Checked == false && chYearly6.Checked == false) == true)
            {
                MassageSummery("IIS509");
            }

            else
            {
                if (Request.QueryString["a"] == "u")
                {
                    Update();
                    lblPaymentModeMsg.Text = "";
                    Session["productCode"] = null;
                    Response.Redirect("~/ProductLibrary/SearchProducts.aspx");
                }
                else if (Request.QueryString["a"] == "v")
                {

                }
            }
        }

        protected void btnUploadFile_Click(object sender, EventArgs e)
        {

        }

        protected void btnBrowse_Click(object sender, EventArgs e)
        {

        }

    }


}